package com.gcit.seniorWish.web.modular.importAdmission.controller;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import com.gcit.seniorWish.web.common.persistence.dao.AdmissionInfoMapper;
import com.gcit.seniorWish.web.common.persistence.model.AdmissionInfo;
import com.gcit.seniorWish.web.core.shiro.ShiroUser;
import com.gcit.seniorWish.web.modular.importAdmission.service.IAdmissionInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.gcit.seniorWish.core.base.controller.BaseController;
import com.gcit.seniorWish.core.base.tips.ErrorTip;
import com.gcit.seniorWish.core.base.tips.Tip;
import com.gcit.seniorWish.web.common.constant.factory.PageFactory;
import com.gcit.seniorWish.web.common.persistence.model.ExamineeScore;
import com.gcit.seniorWish.web.core.exception.BizExceptionEnum;
import com.gcit.seniorWish.web.modular.inputMidScore.service.IExamineeScoreService;
import com.gcit.seniorWish.web.modular.inputMidScore.warpper.ExamineeScoreWarpper;

import cn.hutool.core.util.StrUtil;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/importAdmission")
public class ImportAdmissionController extends BaseController {

	@Autowired
	private IExamineeScoreService examineeScoreService ;

	@Autowired
    private IAdmissionInfoService admissionInfoService ;

	private String PREFIX = "/importAdmission/admission" ;
	
	@RequestMapping("")
	public String index(){
		return PREFIX + "/admission.html" ;
	}

    /**
     * 获取中考录取信息导入列表
     * /**
	 * 获取考生列表
	 */
    @SuppressWarnings({ "unchecked", "rawtypes" })
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(@RequestParam(required = false) String examCardNumber , @RequestParam(required = false) String  studentName ,
                       @RequestParam(required = false) String schoolName, HttpServletRequest request) {
        ShiroUser shiroUser = (ShiroUser) request.getSession().getAttribute("shiroUser");
        Page<Map<String, Object>> page = new PageFactory().defaultPage();
        EntityWrapper<AdmissionInfo> ew = new EntityWrapper<AdmissionInfo>() ;

        if(StrUtil.isNotBlank(examCardNumber)){
        	ew.like("exam_card_number",examCardNumber) ;
        }
        if(StrUtil.isNotBlank(studentName)){
        	ew.like("student_name", studentName) ;
        }
        if(StrUtil.isNotBlank(schoolName)){
            ew.like("enter_school_name", schoolName) ;
        }
        admissionInfoService.selectMapsPage(page, ew) ;
        
        page.setRecords((List<Map<String, Object>>)new ExamineeScoreWarpper(page.getRecords()).warp());
        
    	return packForBT(page);

    }
    /**
     * 跳转到新增录取信息
     * @return
     */
    @RequestMapping("/add")
	public String toAdd(){
		return PREFIX + "/admission_add.html" ;
	}
    /**
     * 查看相关录取信息，并且显示在前台
     * @param id
     * @param model
     * @return
     */
    @RequestMapping(value="/edit")
    public Object edit(String id ,Model model){
    	AdmissionInfo admissionInfo = admissionInfoService.selectById(id) ;
    	model.addAttribute("admissionInfo", admissionInfo) ;
    	return PREFIX + "/admission_edit.html" ;
    }
    
    @RequestMapping(value="/update")
    @ResponseBody
    public Object update(AdmissionInfo admissionInfo){
    	admissionInfoService.updateById(admissionInfo) ;
        return SUCCESS_TIP ;
    }
    /**
     *  根据模板导入学生录取息
     */
    @RequestMapping(method = RequestMethod.POST, path="/import")
    @ResponseBody
    public Object ImportMidScore (@RequestPart("file") MultipartFile excelFile ,HttpServletRequest request){
    	try {
    	   ShiroUser shiroUser = (ShiroUser) request.getSession().getAttribute("shiroUser");
			String msg = admissionInfoService.importAdmission(excelFile.getInputStream(),shiroUser);
			if (StrUtil.isBlank(msg)) {
				msg = "导入成功";
			}
			// 返回导入结果
			return new Tip(200, msg);
		} catch (IOException e) {
			e.printStackTrace();
			return new ErrorTip(BizExceptionEnum.SERVER_ERROR.getCode(), BizExceptionEnum.SERVER_ERROR.getMessage());
		}
    }

    @RequestMapping(value="/remove")
    @ResponseBody
    public Object update(@RequestParam String id){
        AdmissionInfo admissionInfo = admissionInfoService.selectById(id);
        System.out.println(admissionInfo.toString());
        admissionInfoService.deleteById(id);
        return SUCCESS_TIP ;
    }
	
	
}
